---
- name: check/create instance
  hosts: localhost
  user: copr
  gather_facts: False

  vars:
  - keypair: buildsys
  - image: ami-0000000e
  - instance_type: m1.builder
  - security_group: builder

  tasks:
  - name: spin it up
    local_action: ec2 keypair={{ keypair }} image={{ image }} type={{ instance_type }} wait=true group={{ security_group }}
    register: inst_res

  - name: get its internal ip b/c openstack is sometimes stupid
    local_action: shell euca-describe-instances {{ inst_res.instances[0].id }} | grep INSTANCE | cut -f 18
    register: int_ip

  - name: add it to the special group
    local_action: add_host hostname={{ int_ip.stdout }} groupname=builder_temp_group

  - name: wait for the host to be hot
    local_action: wait_for host={{ int_ip.stdout }} port=22 delay=5 timeout=600

  - debug: msg="IP={{ int_ip.stdout }}"

- hosts: builder_temp_group
  user: root
  vars:
   - files: files/
  
  tasks:
  - name: edit hostname to be instance name
    action: shell hostname  `curl -s http://169.254.169.254/2009-04-04/meta-data/instance-id`

  - name: install pkgs
    action: yum state=present pkg={{ item }}
    with_items:
    - rsync
    - openssh-clients
    - libselinux-python
    - libsemanage-python

  - name: add repos
    action: copy src={{ files }}/{{ item }} dest=/etc/yum.repos.d/{{ item }}
    with_items:
    - builder.repo
    - epel6.repo

  - name: install additional pkgs
    action: yum state=present pkg={{ item }}
    with_items:
    - mock
    - createrepo
    - yum-utils

  - name: make sure newest rpm
    action: yum name=rpm state=latest

  - name: mockbuilder user   
    action: user name=mockbuilder groups=mock

  - name: mockbuilder .ssh
    action: file state=directory path=/home/mockbuilder/.ssh mode=0700 owner=mockbuilder group=mockbuilder

  - name: mockbuilder authorized_keys
    action: authorized_key user=mockbuilder key='{{ lookup('file', '/home/copr/provision/files/buildsys.pub') }}'

  - name: put updated mock configs into /etc/mock
    action: copy src={{ files }}/mock/{{ item }} dest=/etc/mock
    with_items:
    - site-defaults.cfg
    - epel-5-x86_64.cfg
    - epel-5-i386.cfg
    - fedora-20-x86_64.cfg
    - fedora-20-i386.cfg
    - epel-7-x86_64.cfg

